import { ref } from 'vue';
import bIZuJianWH from '@/api/juecefxpt/bIZuJianWH';

function useTable(type) {
  const tableType = type == 'table' ? 0 : 2;

  const dataTable = ref([]); //表格数据
  const widgetObj = ref(); //组件对象

  async function handleFetch() {
    // const data = dataTable.value;
    // console.log(data);
    const items = dataTable.value;
    const total = dataTable.value.length;

    return { items: items, total: total };
  }
  async function getTableData(id, data) {
    if (tableType == 2) {
      data.xuanZhongWDList = [];
      data.xuanZhongZBList = [];
    }
    data.zuJianTBLX = tableType;
    widgetObj.value = data;
    const string = JSON.stringify(data);

    const res = await bIZuJianWH.generateDataList({
      gongChengSJJID: id,
      zuJianNR: string,
      chaXunQBBZ: data.config.chaXunQBBZ || 0,
    });

    return res;
  }

  return {
    widgetObj,
    dataTable,
    getTableData,
    handleFetch,
  };
}

export default useTable;
